package Test.Test02;

import java.util.Scanner;

public class DVBProgram {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int rechargeAmount = scanner.nextInt();
        int packagePrice = scanner.nextInt();
        int N = scanner.nextInt();

        int[] dp = new int[rechargeAmount + 1];
        dp[0] = rechargeAmount / packagePrice; // 初始观看时长

        for (int i = 0; i < N; i++) {
            int V1 = scanner.nextInt();
            int V2 = scanner.nextInt();
            int V3 = scanner.nextInt();

            for (int j = rechargeAmount; j >= V1; j--) {
                if (dp[j - V1] >= V1) {
                    int maxTimes = V2 == 1 ? j / V1 : 1;
                    for (int k = 1; k <= maxTimes; k++) {
                        dp[j] = Math.max(dp[j], dp[j - V1 * k] + V3 * k);
                    }
                }
            }
        }

        System.out.println(dp[rechargeAmount]);

        scanner.close();
    }
}